package com.example.hello.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TaklUtils {

    /**
     * 解析提问的内容
     */
    public static String processIATResult(JSONObject cntJson) throws JSONException {
        JSONObject text = cntJson.optJSONObject("text");
        if (null == text) {
            return "";
        }
        boolean lastResult = text.optBoolean("ls");
        if (!lastResult) {
            return "";
        }
        // 解析拼接此次听写结果
        StringBuilder iatText = new StringBuilder();
        JSONArray words = text.optJSONArray("ws");
        for (int index = 0; index < words.length(); index++) {
            JSONArray charWord = words.optJSONObject(index).optJSONArray("cw");
            for (int cIndex = 0; cIndex < charWord.length(); cIndex++) {
                iatText.append(charWord.optJSONObject(cIndex).opt("w"));
            }
        }
        return iatText.toString();
    }

    /**
     * 解析回答的信息内容
     * @param cntJson
     * @return
     * @throws JSONException
     */
    public static String processNlpResult(JSONObject cntJson) throws JSONException {
        JSONObject intent = cntJson.optJSONObject("intent");
        if (null == intent) {
            return "";
        }
        JSONObject answer = intent.optJSONObject("answer");
        if (null == answer) {
            return "";
        }
        return answer.optString("text");
    }
}
